package org.jeecg.modules.operations.assistant.xcbd.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.jeecg.modules.operations.assistant.xcbd.entity.*;
import org.jeecg.modules.operations.assistant.xcbd.mapper.XcbdCollectionRecordMapper;
import org.jeecg.modules.operations.assistant.xcbd.mapper.XcbdContractConfigMapper;
import org.jeecg.modules.operations.assistant.xcbd.mapper.XcbdWorkflowMapper;
import org.jeecg.modules.operations.assistant.xcbd.mapper.XcbdSalesContractMapper;
import org.jeecg.modules.operations.assistant.xcbd.service.IXcbdSalesContractService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description: 销售合同
 * @Author: jeecg-boot
 * @Date:   2024-12-21
 * @Version: V1.0
 */
@Service
public class XcbdSalesContractServiceImpl extends ServiceImpl<XcbdSalesContractMapper, XcbdSalesContract> implements IXcbdSalesContractService {

	@Autowired
	private XcbdSalesContractMapper xcbdSalesContractMapper;
	@Autowired
	private XcbdCollectionRecordMapper xcbdCollectionRecordMapper;
	@Autowired
	private XcbdWorkflowMapper xcbdWorkflowMapper;
	@Autowired
	private XcbdContractConfigMapper contractConfigMapper;
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		xcbdCollectionRecordMapper.deleteByMainId(id);
		xcbdWorkflowMapper.deleteByMainId(id);
		xcbdSalesContractMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			xcbdCollectionRecordMapper.deleteByMainId(id.toString());
			xcbdWorkflowMapper.deleteByMainId(id.toString());
			xcbdSalesContractMapper.deleteById(id);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveSalesContract(XcbdSalesContract salesContract) {
		if (salesContract.getContractAmount().doubleValue()<=5000){
			salesContract.setContractStatus(1);
			salesContract.setProcessStatus("3");  //审批完成
			salesContract.setOntractNumber(getDailySequence());  // 设置合同编号
			salesContract.setCollectionAmount(salesContract.getContractAmount());  // 待回款金额
			xcbdSalesContractMapper.insert(salesContract);
			XcbdWorkflow workflow = new XcbdWorkflow();
			workflow.setStatus("创建");
			workflow.setContent("创建合同");
			workflow.setWorkOrderId(salesContract.getId());
			xcbdWorkflowMapper.insert(workflow);
			workflow = new XcbdWorkflow();
			workflow.setStatus("自动审批");
			workflow.setContent("小于等于五千");
			workflow.setWorkOrderId(salesContract.getId());
			xcbdWorkflowMapper.insert(workflow);
		}else {
			salesContract.setProcessInitiator("全忠");
			salesContract.setProcessStatus("1");  //待副总经理审批
			salesContract.setOntractNumber(getDailySequence());  // 设置合同编号
			salesContract.setCollectionAmount(salesContract.getContractAmount());  // 待回款金额
			xcbdSalesContractMapper.insert(salesContract);
			XcbdWorkflow workflow = new XcbdWorkflow();
			workflow.setStatus("创建");
			workflow.setContent("创建合同");
			workflow.setWorkOrderId(salesContract.getId());
			xcbdWorkflowMapper.insert(workflow);
			workflow = new XcbdWorkflow();
			workflow.setStatus("执行中");
			workflow.setContent("副总经理审批");
			workflow.setWorkOrderId(salesContract.getId());
			xcbdWorkflowMapper.insert(workflow);
		}
//		return SqlHelper.retBool(this.getBaseMapper().insert(salesContract));
	}

	@Override
	public List<YearCountInfo> getYearCountInfo() {
		return xcbdSalesContractMapper.selectYearCountInfo();
	}

	private static final AtomicInteger SEQUENCE = new AtomicInteger(0);
	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
	private static String currentDate;

	public synchronized String getDailySequence() {
		XcbdContractConfig contractConfig = contractConfigMapper.selectById("1873768051529637889");
		String today = DATE_FORMAT.format(new Date());
		int sequence = contractConfig.getSequenceId()+1; // 递增
		contractConfig.setSequenceId(sequence);
		contractConfigMapper.updateById(contractConfig);
		return String.format(contractConfig.getPrefix()+"%s%0"+contractConfig.getSuffixDigit()+"d", today, sequence);
	}

	@Override
	public List<YearCountInfo> getMonthCountInfo() {
		return xcbdSalesContractMapper.selectMonthCountInfo();
	}

	@Override
	public List<YearCountInfo> getWeekCountInfo() {
		return xcbdSalesContractMapper.selectWeekCountInfo();
	}

	@Override
	public XcbdSalesContract getSalesContractInfo(String orderNumber) {
		return xcbdSalesContractMapper.selectSalesContractInfo(orderNumber);
	}
}
